
package com.gp.adapter;

import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gp.R;
import com.gp.model.MywayGrideItem;

public class GrideViewAdapeter extends BaseAdapter {

    private List<MywayGrideItem> mGrideItems;

    private LayoutInflater layoutInflater;

    public GrideViewAdapeter( Context pContext, List<MywayGrideItem> pGrideItems ) {

        this.mGrideItems = pGrideItems;
        // 取得xml里定义的view
        layoutInflater = LayoutInflater.from(pContext);

    }

    public void addListItems(MywayGrideItem pGrideItem) {

        mGrideItems.add(pGrideItem);
    }

    public void removeItem(int position) {
        mGrideItems.remove(position);
    }

    public int getCount() {
        return mGrideItems.size();
    }

    public Object getItem(int position) {
        return mGrideItems.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.myway_gridview_item,
                    null);
        }

        MywayGrideItem qGrideItem = mGrideItems.get(position);

        TextView titleView = (TextView) convertView
                .findViewById(R.id.tv_myway_gvitem_title);
        titleView.setText(qGrideItem.getTitle());

        TextView contentView = (TextView) convertView
                .findViewById(R.id.tv_myway_gvitem_content);
        contentView.setText(qGrideItem.getConten());

        TextView timeView = (TextView) convertView
                .findViewById(R.id.tv_myway_gvitem_Time);

        // 设置时间格式
        String datetime = DateFormat.format("yyyy/MM/dd kk:mm",
                Long.parseLong(qGrideItem.getTime())).toString(); // time的格式

        timeView.setText(datetime);
        return convertView;
    }

}
